<template>
  <a-card :bordered="false" class="card-area">
    <div class="table-page-search-wrapper">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
              <a-form-item label="学号">
                <a-input v-model="queryParam.xh"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="姓名">
                <a-input v-model="queryParam.xm"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="性别">
                <a-select allowClear v-model="queryParam.xbm">
                  <a-select-option v-for="i in TYPE0011" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="所在院系">
                <a-select :disablec="selectMenu.yx.length === 1" allowClear showSearch :filterOption="filterOption" v-model="queryParam.ssyxm" @change="yxChange">
                  <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="所在专业">
                <a-select allowClear showSearch :filterOption="filterOption" v-model="queryParam.sszym">
                  <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zyh}} {{ i.zymc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="年级">
                <a-select allowClear v-model="queryParam.sznj">
                  <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="12">
                <a-form-item label="留学国别">
                  <a-select allowClear v-model="queryParam.lxgbm" showSearch :filterOption="filterOption">
                    <a-select-option v-for="i in TYPE0015" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <!-- <a-col :md="6" :sm="12">
                <a-form-item label="国外导师姓名">
                  <a-input v-model="queryParam.gwdsxm"/>
                </a-form-item>
              </a-col> -->
              <a-col :md="6" :sm="12">
                <a-form-item label="国内导师姓名">
                  <a-input v-model="queryParam.gndsxm"/>
                </a-form-item>
              </a-col>
               <a-col :md="6" :sm="12">
                <a-form-item label="项目状态">
                  <a-select allowClear v-model="queryParam.xmzt">
                    <a-select-option value="0">项目中</a-select-option>
                    <a-select-option value="1">项目结束</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
          </template>
          <a-col :md="24" :sm="24">
            <span class="table-page-search-submitButtons" >
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <!-- <a-button type="primary" >批量审核</a-button> -->
              <a-button style="margin-left: 8px" @click="() => queryParam = {ssyxm: selectMenu.yx.length === 1 ? queryParam.ssyxm : ''}">重置</a-button>
              <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <s-table
      v-if="initDone"
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :data="loadData"
      :alert="{show :true}"
      :pageSize="parameter.pageSize"
      :pageNum="parameter.pageNum"
      :scroll="{x: 1600}"
    >
      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
      </span>
      <template slot="gnds" slot-scope="a,b">
          <span>{{b.teachers?b.teachers.map(item=>item.xm).join(','):''}}</span>
        </template>
      <template slot="xh" slot-scope="a,b">
        <a @click="direct(b)">{{a}}</a>
      </template>
    </s-table>

    <audit
      ref="audit"
      :visible.sync="visible"
      :editId.sync="editId"
      @cancel="visible = false"
      @create="handleCreate"
    />
  </a-card>
</template>

<script>
// import moment from 'moment'
import { STable } from '~'
import { mapState } from 'vuex'
import audit from './audit'

export default {
  name: 'TableList',
  components: {
    STable,
    audit
  },
  data () {
    return {
      // 高级搜索 展开/关闭
      advanced: true,
      // 查询参数
      queryParam: {},
      postParams: {},
      // 表头
      columns: [
        {
            title: '序号',
            scopedSlots: {customRender: 'serial'},
            width: 60,
            // fixed:'left'
          },
          {
            title: '学号',
            dataIndex: 'xh',
            scopedSlots: {customRender: 'xh'},
            width: 120,
            // fixed:'left'
          },
          {
            title: '姓名',
            dataIndex: 'xm'
          },
          {
            title: '院系',
            dataIndex: 'ssyxmc'
          },
          {
            title: '专业',
            dataIndex: 'sszymc'
          },
          {
            title: '年级',
            dataIndex: 'sznj'
          },
          {
            title: '留学国别',
            dataIndex: 'lxgbmc'
          },
          {
            title: '国外导师',
            dataIndex: 'gwdsxm'
          },
          {
            title: '国内导师',
            // dataIndex: 'dedsxm',
            scopedSlots: {customRender: 'gnds'},
          },
          {
            title: '项目状态',
            dataIndex: 'xmzt',
            customRender: (value, row, index) => {
              if(value == '0'){
                  return '项目中'
              }else{
                  return '项目结束'
              }
            }
          },
      ],
        parameter: {
          pageNo: 1,
          pageSize: 10
        },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.overseas.supplementList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectedRowKeys: [],
      selectedRows: [],

      // custom table alert & rowSelection
      options: {
        alert: { show: true }
      },
      optionAlertShow: true, // 是否显示alert，已选中
      visible: false,
      confirmLoading: false,
      editId: null,
      selectMenu: {
        yx: [], // 院系
        zy: [] // 所属专业
      },
      initDone: false,
    }
  },
  computed: {
    ...mapState({
      TYPE0311: state => state.app['TYPE0311'], // 学业奖学金等级
      TYPE0312: state => state.app['TYPE0312'], // 助研奖学金等级
      TYPE0126: state => state.app['TYPE0126'], // 年级
      TYPE0015: state => state.app['TYPE0015'], // 学生类别
      TYPE0056: state => state.app['TYPE0056'], // 录取类别
      TYPE0011: state => state.app['TYPE0011'] // 入学方式
    })
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      let pageParams = vm.$store.state.app.pageParams || {}
      if (from.path === '/student/stuStatus/search/stuView' && pageParams.path === vm.$route.path) {
        vm.queryParam = pageParams.params
        vm.parameter = pageParams.pagination
        return vm.initSelectMenu(1)
      }
      vm.initSelectMenu()
    })
  },
  beforeRouteLeave (to, from, next) {
    if (to.path === '/student/stuStatus/search/stuView') {
      this.$store.dispatch('app/setPageParams', {
        path: this.$route.path,
        params: this.queryParam,
        pagination: this.parameter
      })
    }
    next()
  },
  watch:{
    '$route.path'(newVal,oldVal){
      console.log(11111222)
      this.$nextTick(()=> {
        this.$refs.table.refresh()
      })
    }
  },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0011')
    this.$store.dispatch('app/setDictionary', 'TYPE0015')
    this.$store.dispatch('app/setDictionary', 'TYPE0126')
    this.$store.dispatch('app/setDictionary', 'TYPE0311')
    this.$store.dispatch('app/setDictionary', 'TYPE0312')
    this.$store.dispatch('app/setDictionary', 'TYPE0056')
    
  },
  
  methods: {
    initSelectMenu (notFirst = 0) {
      let allRequest = [
        this.$api.base.collegeAll(),
        this.$store.dispatch('app/sysInfo')
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        if (!notFirst) {
          // this.$set(this.queryParam, 'sznj', res[1].currentGrade)
        }
        if(this.selectMenu.yx && this.selectMenu.yx.length == 1){
          this.$set(this.queryParam, 'ssyxm', this.selectMenu.yx[0].dwh)
          this.yxChange(this.queryParam.ssyxm)
        }
        this.initDone = true
      })
    },
    handleCreate () {
      const form = this.$refs.audit.form
      form.validateFields((err, values) => {
        if (err) {
          return
        }
        this.$api.student.supplementCheck(Object.assign({xh: this.xh}, values)).then(res => {
          this.$refs.table.refresh(false)
        })
        form.resetFields()
        this.visible = false
      })
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    // onSelectChange (selectedRowKeys, selectedRows) {
    //   this.selectedRowKeys = selectedRowKeys
    //   this.selectedRows = selectedRows
    // },
    // openAdd () {
    //   if (!this.selectedRowKeys.length) {
    //     this.$message.warning('请选择需要审核的学生')
    //     return
    //   }
    //   this.xh = this.selectedRows.map(v => {
    //     return v.xh
    //   }).toString()
    //   this.visible = true
    //   this.$refs.audit.form.resetFields()
    // },
    openEdit (row) {
      this.visible = true
      this.xh = row.xh
      this.$refs.audit.form.resetFields()
    },
    yxChange (code) {
      this.$set(this.queryParam, 'sszym', '')
      this.$api.base.majorAll({lsdwh: code}).then(res => {
        this.$set(this.selectMenu, 'zy', res.data)
      })
    },
    direct (data) {
      this.$router.push(`/overseas/stuStatus/search/stuView?xh=${data.xh}`)
    }
  }
}
</script>
